﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OracleClient;
using PPAP.Model;
using System.Data;

namespace PPAP.DAO
{
    public class DepartmentDAO
    {
        public DepartmentDAO() { }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int  Add(Department model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "DEPARTMENT");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DEPARTMENT(");
            strSql.Append("ID,DEPARTMENT,DESCRIPTION,CREATETIME,CREATEDBY)");
            strSql.Append(" values (");
            strSql.Append(":ID,:DEPARTMENT,:DESCRIPTION,:CREATETIME,:CREATEDBY)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":DEPARTMENT", OracleType.VarChar,50),
					new OracleParameter(":DESCRIPTION", OracleType.VarChar,1000),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":CREATEDBY", OracleType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.DEPARTMENT;
            parameters[2].Value = model.DESCRIPTION;
            parameters[3].Value = model.CREATETIME;
            parameters[4].Value = model.CREATEDBY;


           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters)>0?model.ID:0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Department model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DEPARTMENT set ");
            strSql.Append("ID=:ID,");
            strSql.Append("DEPARTMENT=:DEPARTMENT,");
            strSql.Append("DESCRIPTION=:DESCRIPTION,");
            strSql.Append("CREATETIME=:CREATETIME,");
            strSql.Append("CREATEDBY=:CREATEDBY");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":DEPARTMENT", OracleType.VarChar,50),
					new OracleParameter(":DESCRIPTION", OracleType.VarChar,1000),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":CREATEDBY", OracleType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.DEPARTMENT;
            parameters[2].Value = model.DESCRIPTION;
            parameters[3].Value = model.CREATETIME;
            parameters[4].Value = model.CREATEDBY;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DEPARTMENT ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

          return  DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Department GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,DEPARTMENT,DESCRIPTION,CREATETIME,CREATEDBY from DEPARTMENT ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            Department model = new Department();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.DEPARTMENT = ds.Tables[0].Rows[0]["DEPARTMENT"].ToString();
                model.DESCRIPTION = ds.Tables[0].Rows[0]["DESCRIPTION"].ToString();
                if (ds.Tables[0].Rows[0]["CREATETIME"].ToString() != "")
                {
                    model.CREATETIME = DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
                }
                model.CREATEDBY = ds.Tables[0].Rows[0]["CREATEDBY"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,DEPARTMENT,DESCRIPTION,CREATETIME,CREATEDBY,rownum ");
            strSql.Append(" FROM DEPARTMENT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取所有职位
        /// </summary>
        /// <returns></returns>
        public DataSet GetRoles()
        {
            string sql = "select rolesid,rolename,des from roles";
            return DbHelperOra.Query(sql);
        }
    }
}
